<template>
  <div>
    <div style="margin-right: 500px">
      <el-input style="width: 200px" placeholder="请输入名称"  v-model="name" clearable></el-input>
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
      <el-button class="ml-5" type="primary" @click="load"><el-icon><Search /></el-icon>搜索</el-button>
      <el-button type="primary" @click="handleAdd"><el-icon><CirclePlus /></el-icon>新增 <i class="el-icon-circle-plus-outline"></i></el-button>
<!--      <el-button type="warning" @click="reset">重置</el-button>-->
    </div>

    <el-table :data="state.tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="price" label="价格" />
      <el-table-column prop="nums" label="库存" />
      <el-table-column prop="unit" label="库存单位" />
      <el-table-column label="图片" >
        <template #default="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="status" label="状态" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)"><el-icon><Edit /></el-icon>编辑</el-button>
          <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button link type="danger" @click="handleDelete"><el-icon><Delete /></el-icon>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          :background = "true"
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 10, 20]"
          large
          :disabled="false"
          layout="total, sizes, prev, pager, next, jumper"
          :total=total
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog v-model="dialogFormVisible" title="商品信息" width="40%">
      <el-form :model="state.form" :rules="state.rules" ref="ruleFormRef" style="width: 85%" label-width="120px">
        <el-form-item label="商品名"  prop="name">
          <el-input v-model="state.form.name" autocomplete="off" placeholder="请输入商品名"/>
        </el-form-item>
        <el-form-item label="价格"  prop="price">
          <el-input v-model="state.form.price" autocomplete="off" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item prop="img" label="图片">
          <el-upload action="http://localhost:9090/file/upload" ref="img" :on-success="handleImgUploadSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="描述"  prop="description">
          <el-input type="textarea" v-model="state.form.description" autocomplete="off" placeholder="请输入商品描述"/>
        </el-form-item>
        <el-form-item label="库存"  prop="nums">
          <el-input  v-model="state.form.nums" autocomplete="off" placeholder="请输入商品数量"/>
        </el-form-item>
        <el-form-item label="单位"  prop="unit">
          <el-input v-model="state.form.unit" autocomplete="off" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="状态"  prop="status">
          <el-input  v-model="state.form.status" autocomplete="off" placeholder="请输入状态"/>
        </el-form-item>

      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          保存
        </el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {Edit, Delete, Plus, Search, CirclePlus} from '@element-plus/icons-vue'
import {getCurrentInstance, reactive, ref} from "vue";
import request from "../request.js";
import {ElMessage, ElNotification} from "element-plus";
const {proxy} = getCurrentInstance()
const state = reactive({
  tableData:[],
  form:{}
})

const currentPage = ref(1)
const pageSize = ref(3)
const total = ref(0)
const name = ref('')
const load = () =>{
  request.get("/goods/page", {
    params:{
      currentPage: currentPage.value,
      pageSize: pageSize.value,
      name: name.value,
    }
  }).then(res => {
    if(res.code==='200'){
      state.tableData = res.data.list
      total.value = res.data.total
      console.log(total.value)
    }
  })
}
load()

const handleSizeChange = (val) => {
  pageSize.value = val
  load()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  load()
}

const dialogFormVisible = ref(false)
const handleAdd = () => {
  dialogFormVisible.value = true
  state.form = {} //初始化数据
}

const handleEdit = (row) => {
  dialogFormVisible.value = true
  state.form = JSON.parse(JSON.stringify(row))
}

const handleDelete = (id) => {
  request.delete("/goods/" + id).then(res => {
    if(res.code==='200'&&res.data) {
      ElNotification.success("删除成功")
    }else{
      ElMessage.error("删除失败")
    }
    load()
  })
}
const handleImgUploadSuccess = (res) => {
  state.form.img = "http://localhost:8080/file/" + res.data
  console.log(res)
}

const save = () => {
  proxy.$refs.ruleFormRef.validate((valid) => {
    if(valid){
      //console.log(state.form)
      if(state.form.id){//id存在为编辑
        request.put("/goods", state.form).then(res => {
          if(res.code==='200'){
            ElNotification({
              type:'success',
              message:'保存成功'
            })
            dialogFormVisible.value = false
            load()
          }else{
            ElNotification.error(res.msg);
          }
        })
      }else{//新增
        // state.form.password = "123"
        request.post("/goods", state.form).then(res => {
          if(res.code=='200'){
            ElNotification({
              type:'success',
              message:'保存成功'
            })
            dialogFormVisible.value = false
            load()
          }else{
            ElMessage.error(res.msg);
          }
        })
      }
    }
  })
}
</script>

<style scoped>

</style>